<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_store_coupon\Entity\ShusyStoreCoupon;

class CouponItem extends AbstractBaseModel {

  protected function getEntityType() {
    return 'shusy_store_coupon';
  }

  protected function setValue() {
    if ($this->entity instanceof ShusyStoreCoupon) {
      $value = [
        'id' => (int) $this->entity->id,
        'title' => (string) $this->entity->title,
        'amount' => '¥' . $this->formatYuan($this->entity->amount),
        'infos' => $this->getInfos($this->entity),
      ];
      $this->merge($value);
    }
  }

  protected function getInfos(ShusyStoreCoupon $coupon) {
    $list = [];
    if ($coupon->condition_min > 0) {
      //满￥100运费可用
      $list[] = ['desc' => t('满 ￥@min 运费可用', ['@min' => $this->formatYuan($coupon->condition_min)])];
    }
    if ($coupon->days > 0) {
      $list[] = ['desc' => t('领取后 @days 天内可用', ['@days' => $coupon->days])];
    }
    return $list;
  }

  protected function formatYuan($amount) {
    return bcdiv($amount, 100, 2) + 0;
  }
}
